﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace MyTest
{
    public partial class LoadFrm : Form
    {
        SendOrPostCallback callback = null;
        public delegate void SetValueMessMethod(int value,string mess);
        public SetValueMessMethod SetValueMethod;
        object target = null;
        public LoadFrm()
        {
            InitializeComponent();
            SetValueMethod = new SetValueMessMethod(SetValueMessBody);
        }

        public void RegisterMesthod(SendOrPostCallback method, object obj) 
        {
            callback = method;
            target = obj;
        }

        public void SetValueMessBody(int value,string mess) 
        {
            MainBar.Value = value;
            lblMess.Text = mess;
            lblMess.Refresh();
            MainBar.Invalidate();
        }

        private void Form4_Load(object sender, EventArgs e)
        {
            SynchronizationContext sc = SynchronizationContext.Current;
            Thread thread = new Thread(Run);
            thread.Start(sc);
        }

        private void Run(object obj) 
        {
            SynchronizationContext sc = obj as SynchronizationContext;
            sc.Post(callback, target);
        }
    }
   
    public class MethodBody 
    {
        public MethodBody(SendOrPostCallback method, object obj) 
        {
            this.obj = obj;
            this.method = method;
        }
        public object obj { get; set; }
        public SendOrPostCallback method { get; set; }
    }
}
